<?php
class AgentAction extends MemberAction{
	
	public $db;
	public $userinfo;
	public $pagezise=10;
	
	public function _initialize(){
		parent::_initialize();
		$this->db=D('Member');
		$this->userinfo = $this->db->alias('m')->join("left join __MEMBER_GROUP__ as g on g.id=m.group_id")->where(array('m.id'=>$this->memberInfo['id'],'m.site_id'=>__SITEID__))->Field("g.title as groupname,m.*")->find();
	}
	
	//会员管理
	public function member(){
		$model = $this->db=D('Member');
		$map['pid'] = $this->memberInfo['id'];
		if($_GET['status']=="1"){
			$map['status'] = "1";
		}
		if($_GET['status']=="0"){
			$map['status'] = "0";
		}
		//$memberList = $model->where($map)->select();
		
		import ( 'ORG.Util.Page' );
		$count=$model->where($map)->count();
		$p=new Page($count,$this->pagezise);
		$memberList = $model->where($map)->order('id desc')->limit($p->firstRow.','.$p->listRows)->select();
		$this->pages = $p->show();		
		$this->assign("MemberList",$memberList);
		$this->assign("status",$map['status']);
		$this->display();
	}
	
	//添加会员
	public function memberadd(){
		if(isset($_POST['add'])){						
			$_POST['site_id']=__SITEID__;
			$memberModel=D('Member');
			
			if($_POST['username']==""){
				$this -> error("请填写用户名！");
				exit();
			}
			if($_POST['password']==""){
				$this -> error("请填写登录密码！");
				exit();
			}
			
			if($_POST['password']!=$_POST['entpassword']){
				$this -> error("两次密码填写不一致！");
				exit();
			}
			
			$_POST['password']=md5($_POST['password']);
			$_POST['group_id']=3;
			$_POST['pid']=$this->userinfo['id'];
			
			if(!$memberModel->create()){
				$this->error($memberModel->getError());
				exit();
			}
			$id = $memberModel->add();
			if(false===$id){
				$this->error($memberModel->getError());
				exit();
			}else{
				$data = $memberModel->where(array('id' => $id))->find();
				$this->success('添加成功',U('Member/Agent/member'));
				exit();
			}
		}
		
		
		$this->display();
	}
	
	//修改会员信息
	public function memberedit(){
		$id = $_GET['id'];
		if($id==""){
			$this->error("参数错误！");
			exit();
		}
		if(isset($_POST['edit'])){
			$memberModel=D('Member');
			//$_POST['pid']=$id; 
			
			$data['nickname'] = $_POST['nickname'];
			if($_POST['password']!=''){
				if($_POST['password']!=$_POST['entpassword']){
					$this->error("两次密码不一致！");
					exit();
				}
				$data['password']=md5($_POST['password']);
			}					
			$data['tel'] = $_POST['tel'];
			$data['address'] = $_POST['address'];
			$data['QQ'] = $_POST['QQ'];
			$data['E-mail'] = $_POST['E-mail'];
			
			$is = $memberModel->where('id='.$id)->save($data);
			if(false===$is){
				$this->error($memberModel->getError());
				exit();
			}else{
				$this->success('修改成功',U('Member/Agent/member'));
				exit();
			}
		}
		
		$memberInfo = D('Member')->where(array('id'=>$id))->find();			
		
		$this->assign('info',$memberInfo);
		$this->display();
	}
	
	//下订单
	public function membersetorder(){
		$id = $_GET['id'];
		if($id==""){
			$this->error("参数错误！");
			exit();
		}
		$memberInfo = D('Member')->where(array('id'=>$id))->find();	
		if($memberInfo['status']!=1){
			$this->error("该用户未通过审核，不可下单！");
			exit();
		}
		
		//$_COOKIE['member_order_uid'] = $id;
		cookie("member_order_uid",$id);
		header("location:".U('Home/Product_category/index'));
	}
	
	//订单管理
	public function order(){
		
		$modelMember =  D('Member');
		$modelOrder = D('Order');
		$childMemberId = $modelMember->field('id')->where(array('pid'=>$this->memberInfo['id']))->select();
		$map['memeber_id'] = array('in',$childMemberId);
		if($_GET['status']=="1"){
			$map['o.status'] = "1";
		}
		if($_GET['status']=="0"){
			$map['o.status'] = "0";
		}
		//$orderList = $modelOrder->where($map)->order('id desc')->select();
		//dump($orderList);
		//dump($childMemberId);
				
		import ( 'ORG.Util.Page' );
		$count=$modelOrder->alias('o')->join('left join __MEMBER__ as m on o.member_id=m.id')->where($map)->count();
		$p=new Page($count,$this->pagezise);
		$orderList = $modelOrder->alias('o')->join('left join __MEMBER__ as m on o.member_id=m.id')->field('o.*,m.username')->where($map)->order('o.id desc')->limit($p->firstRow.','.$p->listRows)->select();
		$this->pages = $p->show();	
		
		//dump($orderList);
		$this->assign("status",$map['o.status']);
		$this->assign('OrderList',$orderList);
		$this->display();
	}
	
	//订单详情
	public function orderdetail(){
		$oid = $_GET['oid'];
		if(empty($oid) && !is_numeric($oid)){
			$this->error("Error~~");
			exit();
		}
		
		$orderModel = D('Order');
		$saleModel = D('Sale');
		$orderInfo = $orderModel -> where(array('id'=>$oid)) -> find();
		//dump($orderInfo);
		$saleInfo = $saleModel->alias('s') -> join('left join __PRODUCT__ as p on s.product_id=p.id') -> field('s.*,p.title') -> where(array('order_id'=>$oid))->select();
		//dump($saleInfo);
		foreach($saleInfo as $k=>$v){
			//echo $v['attr'];
			$attr = split(":",$v['attr']);
			$attr = $attr[1];
			$attr = str_replace(";",",",$attr);
			//echo $attr;
			$map['id'] = array("in",$attr);
			$attvlist = D('attr_val')->where($map)-> field('val') -> select();
			foreach($attvlist as $m=>$n){
				if(!empty($saleInfo[$k]['attrv'])) $saleInfo[$k]['attrv'] .=",";
				$saleInfo[$k]['attrv'] .= $n['val'];
			}
		}
		//dump($saleInfo);
		
		$this->assign("orderInfo",$orderInfo);
		$this->assign("saleList",$saleInfo);
		$this->display();
	}
}